package pl.edu.agh.persdoc.resultsws;
import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.restlet.data.MediaType;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import pl.edu.agh.mobilne.DomRepresentationResource;

public class ResultResource extends ServerResource implements DomRepresentationResource{

	
	/*@Override
    public Representation put(Representation entity) throws ResourceException{
		 System.out.println("PUT!");
         if (entity.getMediaType().equals(MediaType.TEXT_XML,true)) {
               System.out.println("new result will be stored");
               DomRepresentation document = new DomRepresentation(entity);
               try {
   				document.write(System.out);
   			} catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
               Result result=createResultObjectFromXML(document);
               storeResult(result);
             
         }
         else{
        	 System.out.println("it is not XML");
         }

        return null;
    }*/ 
	
	private void storeResult(Result result) {
		System.out.println("Storing...");
		ExaminationResultsWebService.getResultRepository().add(result);
		System.out.println("Stored!!!!");
	}

	private Result createResultObjectFromXML(DomRepresentation document) {
		Result result=new Result();
		result.setPatientID(document.getText("/Measurement/patientID"));
		result.setName(document.getText("/Measurement/name"));
		result.setValue(Integer.parseInt(document.getText("/Measurement/value")));
		result.setTimestamp(convertStringToDate(document.getText("/Measurement/timestamp")));	
		return result;									     
	}

	private Date convertStringToDate(String text) {
		
		DateFormat df=DateFormat.getDateInstance();
		Date resultDate=null;
		try {
			resultDate=df.parse(text);
		} catch (ParseException e){ 
		}
		return resultDate;
		
	}
	@Get
	public String retrieve() {
		// TODO Auto-generated method stub
		return null;
	}
	@Put
	public void store(String xmlSource) {
		System.out.println("PUT_2!");
		DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        Document document = null;
		try {
			docBuilder = dbfac.newDocumentBuilder();
			document = docBuilder.parse(new InputSource(new StringReader(xmlSource)));
		} catch (ParserConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DomRepresentation domDocument = new DomRepresentation(MediaType.TEXT_XML, document);
		
         if (domDocument.getMediaType().equals(MediaType.TEXT_XML,true)) {
               System.out.println("new result will be stored");

               System.out.println("doc created");
               
               try {
   				  System.out.println(domDocument.getText());
   			   } catch (IOException e) {
   				  e.printStackTrace();
   			   }
   			   
               Result result = createResultObjectFromXML(domDocument);
               System.out.println("result created");
               storeResult(result);
             
         }
         else{
        	 System.out.println("it is not XML");
         }

	}
	@Delete
	public void remove() {
		// TODO Auto-generated method stub
		
	}
}
